<template>
  <div>
    <el-breadcrumb separator-class="el-icon-arrow-right" style="font-size: 16px">
      <el-breadcrumb-item :to="{ path: '/' }">后台管理</el-breadcrumb-item>
      <el-breadcrumb-item>相册列表</el-breadcrumb-item>
    </el-breadcrumb>

    <el-divider></el-divider>

    <el-table
        :data="tableData"
        border
        style="width: 100%">
      <el-table-column prop="id" label="ID" width="70px" align="center"></el-table-column>
      <el-table-column prop="name" label="名称" width="180px" align="center"></el-table-column>
      <el-table-column prop="description" label="简介" header-align="center"></el-table-column>
      <el-table-column prop="sort" label="手动排序" width="70px" align="center"></el-table-column>
      <el-table-column label="操作" align="center" width="120px">
        <template slot-scope="scope">
          <el-button type="primary" icon="el-icon-edit"
                     circle size="small" @click="handleAlbumEdit(scope.row)"></el-button>
          <el-button type="danger" icon="el-icon-delete"
                     circle size="small" @click="albumDeleteConfirm(scope.row)"></el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-dialog title="修改相册" :visible.sync="dialogFormVisible">
      <el-form :model="form">
        <el-form-item label="相册名称" :label-width="formLabelWidth">
          <el-input v-model="form.name" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="相册简介" :label-width="formLabelWidth">
          <el-input v-model="form.description" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="手动排序" :label-width="formLabelWidth">
          <el-input v-model="form.sort" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="handleAlbumUpdate()" >确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      tableData: [],
      dialogFormVisible: false,
      form: {
        id: '',
        name: '',
        description: '',
        sort: ''
      },
      formLabelWidth: '120px'
    }
  },
  methods: {
    handleAlbumUpdate(){
      let url = 'http://localhost:9080/albums/updateById'
      let formData = this.qs.stringify(this.form)
      this.axios
          .create({'headers': {'Authorization': localStorage.getItem('jwt')}})
          .post(url, formData).then(
          (response)=>{
            let responseBody = response.data
            if (responseBody.state != 20000){
              this.$message.error(responseBody.message)
              this.loadAlbumList();
            }else {
              this.$message({
                message: '修改成功',
                type: 'success'
              });
              this.loadAlbumList();
              this.dialogFormVisible = false
            }
          }
      )
    },
    handleAlbumEdit(album){
      this.dialogFormVisible = true;
      this.axios.get('http://localhost:9080/albums/'+album.id+'/getStandById').then(
          (response)=>{
            let responseBody = response.data;
            if (responseBody.state != 20000){
              this.$message.error(responseBody.message)
              this.loadAlbumList();
            }else {
              this.form = responseBody.data;
            }
          }
      )

    },
    handleAlbumDelete(album){
      console.log("准备删除id为"+album.id+"相册")
      this.axios.post('http://localhost:9080/albums/'+album.id+'/delete').then(
          (response)=>{
            let responseBody = response.data;
            if (responseBody.state != 20000){
              this.$message.error(responseBody.message)
            }else{
              this.$message({
                message: '删除成功',
                type: 'success'
              });
            }
            if(responseBody.state==20000 || responseBody.state == 40400){
              this.loadAlbumList();
            }
          }
      )

    },
    albumDeleteConfirm(album){
      let message = '此操作将永久删除'+album.name+'文件, 是否继续?'
      this.$confirm(message, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.handleAlbumDelete(album);
      }).catch(() => {

      });
    },
    loadAlbumList() {
      let url = "http://localhost:9080/albums"
      console.log(url);
      this.axios
          .create({
            'headers':{
              'Authorization': localStorage.getItem('jwt')
            }
          })
          .get(url).then((response) => {
            let responseBody = response.data;
            this.tableData = responseBody.data;
          }
      )
    }
  },
  mounted() {
    console.log("mounted");
    this.loadAlbumList();
  }
}
</script>
